/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOS_BOX_SRC_OBJECTS_VARIANT_IMPL_H__
#define __XOS_BOX_SRC_OBJECTS_VARIANT_IMPL_H__

#include "../tools/head.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_box
{

    class variant_impl : public xos_stl::mem_item< xos::release_impl< variant_impl >, thread_lock >,
        public i_variant
    {
    public:
        typedef xos::release_impl< variant_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        static int get_xos_item_data( ITEM_DATA *& item_ptr );

    public:
        variant_impl();
        ~variant_impl();

    protected:
        int init_data();

    public:
        int init();
        int term();

    protected:
        const char * to_string( char * lpszValue, int nBufSize, int * lpnSizeNeeded );
        double to_double( double * lpdValue );
        float to_float( float * lpfValue );
        int32_t to_int32( int32_t * lpnValue );

    public:
        const char * str( char * lpszValue, int nBufSize, int * lpnSizeNeeded );
        xos::i_release * obj( xos::i_release ** ppv );
        xos_box::i_list * list( xos_box::i_list ** ppv );
        i_int_property * int_prop( i_int_property ** ppv );
        i_str_property * str_prop( i_str_property ** ppv );
        i_str_property * attr( i_str_property ** ppv );
        v_create fun( v_create * pv );
        double dbl( double * lpdValue );
        float flt( float * lpfValue );
        int32_t i32( int32_t * lpnValue );

        uint64_t * u64_ptr( uint64_t ** lppRet );
        int64_t * i64_ptr( int64_t ** lppRet );
        uint32_t * u32_ptr( uint32_t ** lppRet );
        int32_t * i32_ptr( int32_t ** lppRet );
        uint16_t * u16_ptr( uint16_t ** lppRet );
        int16_t * i16_ptr( int16_t ** lppRet );
        uint8_t * u8_ptr( uint8_t ** lppRet );
        int8_t * i8_ptr( int8_t ** lppRet );
        char * char_array( char ** lppRet, int * lpnSize, int * lpnLen );
        char * bin_ptr( char ** lppRet, int * lpnSize, int * lpnLen );
        double * dbl_ptr( double ** lppRet );
        float * flt_ptr( float ** lppRet );
        bool * bool_ptr( bool ** lppRet );

        int set( const char * lpszValue );
        int set_obj( xos::i_release * pv, bool bAutoRelease );
        int set_list( xos_box::i_list * pv, bool bAutoRelease );
        int set_int_prop( i_int_property * pv, bool bAutoRelease );
        int set_str_prop( i_str_property * pv, bool bAutoRelease );
        int set_attr( i_str_property * pv );
        int set( v_create pv );
        int set( double value );
        int set( float value );
        int set( int32_t value );

        int set( uint64_t * lpRef );
        int set( int64_t * lpRef );
        int set( uint32_t * lpRef );
        int set( int32_t * lpRef );
        int set( uint16_t * lpRef );
        int set( int16_t * lpRef );
        int set_u8( uint8_t * lpRef );
        int set_i8( int8_t * lpRef );
        int set_char_array( char * lpRef, int nBufSize );
        int set_bin( char * lpRef, int nBufSize );
        int set( double * lpRef );
        int set( float * lpRef );
        int set( bool * lpRef );

        VT_TYPE get_type( VT_TYPE * lpType );
        int set_type( VT_TYPE type );
        int set_ref_value( i_variant * pVT );

    protected:
        // 只有这6个独立变量
        bool m_bAutoReleaseObject;

        i_str_property * m_pAttr;
        std::string m_str;

        int m_nArraySize;
        int m_nArrayLen;

        VT_TYPE m_type;

        // 其它变量都是联合
        union
        {
            i_int_property * m_pIntProperty;
            i_str_property * m_pProperty;
            xos::i_release * m_pObject;
            xos_box::i_list * m_pList;
            v_create m_pCreate;
            int32_t m_i32;
            double m_d;
            float m_f;
            uint64_t * m_pu64;
            int64_t * m_pi64;
            uint32_t * m_pu32;
            int32_t * m_pi32;
            uint16_t * m_pu16;
            int16_t * m_pi16;
            uint8_t * m_pu8;
            int8_t * m_pi8;
            char * m_pCharArray;
            char * m_pBinArray;
            bool * m_pbool;
            double * m_pd;
            float * m_pf;
        };
    };

} // so_common

#endif // __XOS_BOX_SRC_OBJECTS_VARIANT_IMPL_H__
